package com.aliwo.controller;

import com.aliwo.service.JobCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.powerjob.common.enums.InstanceStatus;
import tech.powerjob.common.request.query.JobInfoQuery;
import tech.powerjob.common.response.InstanceInfoDTO;
import tech.powerjob.common.response.JobInfoDTO;
import tech.powerjob.common.response.ResultDTO;

import java.util.List;

/**
 * @author:xuyayuan
 * @date:2021/7/26 13:28
 * @description:TODO
 */
@SuppressWarnings("all")
@Api(tags = "Job公共接口模块")
@RestController
@RequestMapping("/powerjob/common")
public class JobCommonController {
    @Autowired
    private JobCommonService commonService;

    //@Autowired
    //private JobInfoFactory jobInfoFactory;
    //
    //@ApiOperation(value = "分页查询")
    //@GetMapping
    //public ResponseEntity<Page<JobInfoResponse>> findPage(@RequestBody QueryJobInfoRequest queryJobInfoRequestrequest,
    //                                                      @CustomPagingParam PagingParam pagingParam){
    //    Page<JobInfoDO> page = commonService.findPage(queryJobInfoRequestrequest,pagingParam);
    //    Page<JobInfoResponse> result = page.map(jobInfoFactory::toResponse);
    //    return ResponseEntity.ok(result);
    //}

    @ApiOperation(value = "复制任务")
    @PostMapping("/{jobId}")
    public ResultDTO<Long> copyJob(@ApiParam(value = "任务ID") @PathVariable Long jobId) {
        ResultDTO<Long> jobInfoDTO = commonService.copyJob(jobId);
        return ResultDTO.success(jobInfoDTO.getData());
    }


    @ApiOperation(value = "查找某个任务")
    @GetMapping("/{jobId}")
    public ResultDTO<JobInfoDTO> fetchJob(@ApiParam(value = "任务ID") @PathVariable Long jobId) {
        ResultDTO<JobInfoDTO> jobInfoDTO = commonService.fetchJob(jobId);
        return ResultDTO.success(jobInfoDTO.getData());
    }

    @ApiOperation(value = "查找所有任务")
    @PostMapping("/fetchAllJob")
    public ResultDTO<List<JobInfoDTO>> fetchAllJob() {
        return ResultDTO.success(commonService.fetchAllJob().getData());
    }

    @ApiOperation(value = "查找所有任务，不包含删除的任务")
    @PostMapping("/fetchAllJobExcludeDelete")
    public ResultDTO<List<JobInfoDTO>> fetchAllJobExcludeDelete() {
        return ResultDTO.success(commonService.fetchAllJobExcludeDelete().getData());
    }

    @ApiOperation(value = "根据查询参数查找任务")
    @PostMapping("/queryJob")
    public ResultDTO<List<JobInfoDTO>> queryJob(@RequestBody JobInfoQuery powerQuery) {
        ResultDTO<List<JobInfoDTO>> jobListResultDTO = commonService.queryJob(powerQuery);
        return ResultDTO.success(jobListResultDTO.getData());
    }

    @ApiOperation(value = "禁用某个任务")
    @GetMapping("/disableJob/{jobId}")
    public ResultDTO<Void> disableJob(@ApiParam(value = "任务ID") @PathVariable Long jobId) {
        commonService.disableJob(jobId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "启用某个任务")
    @GetMapping("/enableJob/{jobId}")
    public ResultDTO<Void> enableJob(@ApiParam(value = "任务ID") @PathVariable Long jobId) {
        commonService.enableJob(jobId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "删除某个任务")
    @DeleteMapping("/deleteJob/{jobId}")
    public ResultDTO<Void> deleteJob(@ApiParam(value = "任务ID") @PathVariable Long jobId) {
        commonService.deleteJob(jobId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "运行某个任务")
    @PostMapping("/runJob")
    public ResultDTO<Long> runJob(@ApiParam(value = "任务ID") @RequestParam(required = true) Long jobId,
                                  @ApiParam(value = "任务实例参数") @RequestParam(required = false) String instanceParams,
                                  @ApiParam(value = "延迟执行时间") @RequestParam(required = false) Long delay) {
        ResultDTO<Long> longResultDTO = commonService.runJob(jobId, instanceParams, delay == null ? 0L : delay);
        return ResultDTO.success(longResultDTO.getData());
    }


    /* ************* Instance 区 ************* */

    @ApiOperation(value = "停止某个任务实例")
    @PostMapping("/stopInstance")
    public ResultDTO<Void> stopInstance(@ApiParam("任务实例ID") @RequestParam Long instanceId) {
        commonService.stopInstance(instanceId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "取消某个任务实例")
    @PostMapping("/cancelInstance")
    public ResultDTO<Void> cancelInstance(Long instanceId) {
        commonService.cancelInstance(instanceId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "重试某个任务实例")
    @PostMapping("/retryInstance")
    public ResultDTO<Void> retryInstance(Long instanceId) {
        commonService.retryInstance(instanceId);
        return ResultDTO.success(null);
    }

    @ApiOperation(value = "查询某个任务实例状态")
    @PostMapping("/fetchInstanceStatus")
    public ResultDTO<Integer> fetchInstanceStatus(Long instanceId) {
        InstanceStatus instanceStatus = InstanceStatus.of(commonService.fetchInstanceStatus(instanceId).getData());
        return ResultDTO.success(instanceStatus.getV());
    }

    @ApiOperation(value = "查询某个任务实例")
    @PostMapping("/fetchInstanceInfo")
    public ResultDTO<InstanceInfoDTO> fetchInstanceInfo(Long instanceId) {
        return ResultDTO.success(commonService.fetchInstanceInfo(instanceId).getData());
    }


}
